package com.chis.netty.http.client;


import com.chis.netty.http.test.ThreadTest2;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion;
import org.apache.http.client.methods.HttpPost;

import java.net.URI;
import java.util.Date;

/**
 * 
 * @Description 
 * @Company zwx
 * @author wlj
 * @version 2019年12月19日
 */
public class HttpClient extends Thread{

    public HttpClient(String name){
        super(name);
    }

    private static String testip = "127.0.0.1";
    private static int testport = 9091;

    private static void sendRequest()throws Exception{
//        URI uri = new URI(testurl);
//        String host = uri.getHost();

        // Configure the client.
        EventLoopGroup group = new NioEventLoopGroup();

        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group).channel(NioSocketChannel.class).handler(new HttpClientInitializer());

        // Make the connection attempt.
        Channel ch = bootstrap.connect(testip, testport).sync().channel();

        // Prepare the HTTP request.
        String content = "hello post";
        FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, testip);
//        request.headers().set(HttpHeaderNames.HOST, host);
        request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
        request.headers().set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);

//        requestToSQLMAPAPI.headers().set(HttpHeaders.Names.HOST, "127.0.0.1");
//        requestToSQLMAPAPI.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
//        requestToSQLMAPAPI.headers().set(HttpHeaders.Names.CONTENT_LENGTH,
//                requestToSQLMAPAPI.content().readableBytes());
//        requestToSQLMAPAPI.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");


        // Send the HTTP request.
        ch.writeAndFlush(request);

        // Wait for the server to close the connection.
        ch.closeFuture().sync();

        // Shut down executor threads to exit.
        group.shutdownGracefully();
    }

    @Override
    public void run() {
        Long l1 = System.currentTimeMillis();
        try {
            HttpClient.sendRequest();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(" 本次耗时："+(System.currentTimeMillis()-l1));
    }

    public static void main(String[] args) {
        try {
            for(int i = 0 ; i < 1 ; i++){
                HttpClient threadRuning = new HttpClient("thread"+i);
                threadRuning.start();
            }

        } catch (Exception e){
            e.printStackTrace();
        }


    }
}

